import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';

class ListViewDemoPage extends StatefulWidget {
  ListViewDemoPage({Key key}) : super(key: key);

  @override
  _ListViewDemoPageState createState() => _ListViewDemoPageState();
}

class _ListViewDemoPageState extends State<ListViewDemoPage> {
  List mList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // setState(() {
    //   _getData();
    // });
    _getData();
  }

  _getData() async {
    var url = "http://a.itying.com/api/productlist";
    Response reslut = await Dio().get(url);
    setState(() {
      print(reslut.data["result"]);
      // String a = reslut.data;
      // print(json.decode(a));
      // this.mList = json.decode(reslut.data)["result"];
      this.mList = reslut.data["result"];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("列表"),
      ),
      body: this.mList.length > 0
          ? ListView.builder(
              itemCount: this.mList.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(this.mList[index]["title"]),
                );
              },
            )
          : Text("加载中..."),
    );
  }
}
